package com.zzyl.serve.service.impl.member;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.member.Reservation;
import com.zzyl.serve.dto.member.ReservationDto;
import com.zzyl.serve.mapper.member.ReservationMapper;
import com.zzyl.serve.service.member.IReservationService;
import com.zzyl.serve.vo.member.ReservationCountBytimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;




    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservationDto 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(ReservationDto reservationDto) {
        //拷贝属性
        System.out.println(reservationDto);
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(reservationDto, reservation);
        //因为都是预约的,所以状态先设置为待报到
        reservation.setStatus(0);
        boolean save = save(reservation);
        return save ? 1 : 0;
    }


    /**
     * 查询当天时间的每个时间段的各个预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<ReservationCountBytimeVo> getCountByTime(Long time) {
        //当前时间
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        //结束时间是一天之后
        LocalDateTime endTime = startTime.plusDays(1);
        List<ReservationCountBytimeVo> countByTime = reservationMapper.getCountByTime(startTime, endTime);
        return countByTime;
    }

    /**
     * 查询取消次数
     *
     * @return
     */

    @Override
    public Integer getCancelledReservationCount() {
        //当前时间的毫秒值
        long time = System.currentTimeMillis();
        //转化为时间格式
        LocalDateTime ldt = LocalDateTimeUtil.of(time);

        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        //然后加一天
        LocalDateTime endTime = startTime.plusDays(1);
        //首先我们取消预约的时候我们的update_by会被修改成当前人的Id
        //我们再根据当前时间就可以查询到当前人的预约状态为2的条数,并不需要去创建sql语句
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(Reservation::getUpdateTime, startTime, endTime)
                .eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, UserThreadLocal.getUserId());
        return (int) count(queryWrapper);

    }

    /**
     * 取消预约次数
     * @param id
     * @return
     */
    @Override
    public int cancelReservation(Long id) {
        //通过Id查询到预约信息
        Reservation reservation = getById(id);
        //然后给状态设置为2,然后再给修改人设置为当前人的登录Id
        reservation.setStatus(2);
        //加个""转为空字符串
        reservation.setUpdateBy(UserThreadLocal.getUserId()+"");
        return updateById(reservation) ? 1 : 0;

    }
    // TODO 定时任务去修改
    @Override
    public void updateReservationStatus() {
        //首先我们定义一个条件构造器,我们筛选出当前时间大约预约时间的且状态为0的
          LambdaQueryWrapper<Reservation> lqw=new LambdaQueryWrapper<>();
          lqw.eq(Reservation::getStatus,0)
                  .le(Reservation::getTime, LocalDateTime.now());
          List<Reservation> list = list(lqw);
          //查不到就说明一切正常
          if(CollectionUtils.isEmpty(list)){

              return;
          }
          //如果有超时的,我们就修改为3过期
           for (Reservation reservation : list){
               reservation.setStatus(3);



           }
           updateBatchById(list);




    }
}
